package com.jxt.app.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import com.jxt.app.entity.base.Apreply;
import com.jxt.app.entity.base.ApreplyExample;
import com.jxt.app.entity.base.Apsharepost;
import com.jxt.app.entity.base.ApsharepostExample;
import com.jxt.app.service.ApReplyService;
import com.jxt.app.service.ApSharePostService;
import com.jxt.app.util.Page;

@Controller
public class ShareController {
	@Resource 
	private ApSharePostService apSharePostService;
	@Resource
	private ApReplyService apReplyService;
	
	/**
	 * 家长分享列表
	 * @param page
	 * @param pageSize
	 * @param requset
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/app/share/list/{userid}/{page}/{pageSize}",method=RequestMethod.GET) 
	public String shareList(@PathVariable long userid,@PathVariable int page,@PathVariable int pageSize,HttpServletRequest requset, Model model) throws UnsupportedEncodingException{
	
		ApsharepostExample example=new ApsharepostExample();
		example.setId((long)0);
		example.setId_Indicator(ApsharepostExample.EXAMPLE_GREATER_THAN);
		Page apnewsList = null;
		try {
			apnewsList = apSharePostService.findListByExample(example, page, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map resultMap=new HashMap();
		resultMap.put("code", "1001");
		resultMap.put("list", apnewsList);
		model.addAttribute("result", resultMap);
		return "megList";
	}
	/**
	 * 根据ID查询分享主题
	 * @param id
	 * @param requset
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/app/share/dt/{id}/{page}/{pageSize}",method=RequestMethod.GET) 
	public String shareDetail(@PathVariable long id,@PathVariable int page,@PathVariable int pageSize,HttpServletRequest requset, Model model) throws UnsupportedEncodingException{
	
		
		Map resultMap=new HashMap();
		ApsharepostExample example=new ApsharepostExample();
		example.setId(id);
		example.setId_Indicator(ApsharepostExample.EXAMPLE_EQUALS);
		
		List<Apsharepost> postList=apSharePostService.listModel(example);
		
		Apsharepost sharePost = null;
		if(postList.size()>0){
			sharePost=postList.get(0);
		}
	
		Page pageList=null;
		ApreplyExample apreplyExample=new ApreplyExample();
		if(null!=sharePost){
			apreplyExample.setShareid(sharePost.getId());
			apreplyExample.setShareid_Indicator(ApreplyExample.EXAMPLE_EQUALS);
			try {
				pageList= apReplyService.findListByExample(apreplyExample, 1, 10);
				resultMap.put("code", "1001");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				resultMap.put("code", "-1001");
				e.printStackTrace();
			}
		}
		


		resultMap.put("post",sharePost );
		resultMap.put("page",pageList);
		model.addAttribute("result", resultMap);
		return "megList";
	}
	/**
	 * 根据分享ID查询回复分页接口
	 * @param id
	 * @param page
	 * @param pageSize
	 * @param requset
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/app/reply/{id}/{page}/{pageSize}",method=RequestMethod.GET)
	public String getReplyByshareId(@PathVariable long id,@PathVariable int page,@PathVariable int pageSize,HttpServletRequest requset, Model model) throws UnsupportedEncodingException{
		
			ApreplyExample apreplyExample=new ApreplyExample();
			Page replyPage=null;
			apreplyExample.setShareid(id);
			apreplyExample.setShareid_Indicator(ApreplyExample.EXAMPLE_EQUALS);
			try {
				replyPage=apReplyService.findListByExample(apreplyExample, page, pageSize);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
			Map resultMap=new HashMap();
			resultMap.put("code", "1001");
			resultMap.put("list", replyPage);
			model.addAttribute("result", resultMap);
		return null;
	
	}
	
	/**
	 * 发表帖子
	 * @param sharepost
	 * @param requset
	 * @param model
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/app/sendSharePost",method=RequestMethod.POST)
	public void sendsharePost(Apsharepost sharepost,HttpServletRequest requset, HttpServletResponse response,Model model) throws IOException{
		Map resultMap=new HashMap();
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		if(sharepost!=null){
			try{
				byte value=0;
				sharepost.setPublishtime(new Date().getTime());
				sharepost.setCanreply(value);
				sharepost.setSysflag(value);
				sharepost.setStatus(value);
				apSharePostService.insertModel(sharepost);
				resultMap.put("code", "1001");
			}catch(Exception e){	
				resultMap.put("code", "-1001");
				
			}
			
		}
		JSONObject obj = new JSONObject();
 		obj.put("result", resultMap);
 		out.println(obj.toString());
 		

	}
	
	/**
	 * 发表回复
	 * @param reply
	 * @param requset
	 * @param model
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/app/sendReply",method=RequestMethod.POST)
	public void sendReply(Apreply reply,HttpServletRequest requset, HttpServletResponse response,Model model) throws IOException{
		Map resultMap=new HashMap();
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		if(reply!=null){
			try{
				byte value=0;
				reply.setReplytime(new Date().getTime());
				reply.setSysflag(value);
				reply.setIsverify(value);
				apReplyService.insertModel(reply);
				resultMap.put("code", "1001");
			}catch(Exception e){	
				resultMap.put("code", "-1001");
				
			}
			
		}
		JSONObject obj = new JSONObject();
 		obj.put("result", resultMap);
 		out.println(obj.toString());
		
	}
}
